package cn.uai.activiti8.approval.service.impl;

import cn.uai.activiti8.approval.bus.AbstractBusinessBus;
import cn.uai.activiti8.approval.service.InfrastructureService;
import cn.uai.activiti8.approval.service.LoanApplicationProcessService;
import cn.uai.activiti8.dto.Result;
import cn.uai.activiti8.entity.ApplyForLevel;
import cn.uai.activiti8.enums.RespCodeEnum;
import com.alibaba.fastjson2.JSONObject;
import jakarta.annotation.Resource;
import org.activiti.engine.ActivitiObjectNotFoundException;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

import static cn.uai.activiti8.enums.RespCodeEnum.TASK_NOT_FOUND;

/**
 * @desc 借款申请流程
 * @author uai
 * @since 2025/8/10 19:50
 * @version 1.0
 */
@Service
public class LoanApplicationProcessServiceImpl extends InfrastructureServiceImpl implements LoanApplicationProcessService {

    private static final String PROCESS_DEFINITION_ID = "multi-node-example-key:1:777503";
    private static final String PROCESS_DEFINITION_KEY = "multi-node-example-key:1:777503";

    @Resource
    private InfrastructureService infrastructureService;

    @Override
    public ProcessInstance start(String body) {
        ApplyForLevel apply = JSONObject.parseObject(body, ApplyForLevel.class);
        Map<String, Object> variables = new HashMap<>();
        dynamicAssignStartVariables(apply, variables);
        return infrastructureService.startByKey(PROCESS_DEFINITION_KEY, variables);
    }

    @Override
    public String complete(String body) {
        JSONObject bodyJson = JSONObject.parseObject(body);
        Map<String, Object> variables = new HashMap<>();
        String taskId = bodyJson.getString("taskId");
        Integer actionType = bodyJson.getInteger("actionType");
        switch (actionType) {
            case 1 -> variables.put("isPass", true);
            case 2, 3 -> variables.put("isPass", false);
        }
        dynamicAssignCompleteVariables(actionType, variables);
        try {
            taskService.complete(taskId, variables);
        } catch (ActivitiObjectNotFoundException e) {
            return Result.exception(TASK_NOT_FOUND.getCode(), "任务 taskId [" + taskId + "] 已完成，或输入的taskId不存在");
        }
        return Result.success();
    }

    @Override
    public void dynamicAssignStartVariables(Object original, Map<String, Object> variables) {
        ApplyForLevel apply = (ApplyForLevel) original;
        // initiator 是Activiti存储发起人信息的变量名
        variables.put("initiator", apply.getApplicant());
        variables.put("applicant", apply.getApplicant());
        variables.put("type", apply.getType());
        variables.put("reason", apply.getReason());
        variables.put("startDate", apply.getStartDate());
        variables.put("endDate", apply.getEndDate());
        variables.put("userNode1", "681372686954565");
    }

    @Override
    protected void dynamicAssignCompleteVariables(Object original, Map<String, Object> variables) {
        variables.put("actionType", original);
//        variable.put("assignProductLineManager", "681374462742597");
        variables.put("userNode2", "681374462742597");
        variables.put("userNode3", "682799093862469");
        variables.put("userNode4", "690028525338693");
        variables.put("userNode5", "700661117624389");
    }
}
